import java.sql.*;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
//        m2();
//        m3();
        m4();
//        m5();
    }
    private static void m3() {
        Connection connection=null;
        PreparedStatement preparedStatement=null;
        Scanner scanner=new Scanner(System.in);
        String name=scanner.nextLine();
        String pass=scanner.nextLine();
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
            String url="jdbc:mysql://127.0.0.1:3306/jdbc1?characterEncoding=utf-8";
            try {
                connection = DriverManager.getConnection(url, "root", "130258");
//                if(connection !=null){
//                    System.out.println("链接成功");
//                }
                String sql="insert into user(name,pass) values (?,?);";
                preparedStatement = connection.prepareStatement(sql);
                preparedStatement.setString(1,name);
                preparedStatement.setString(2,pass);
                int result= preparedStatement.executeUpdate();
                if(result>0){
                    System.out.println("插入成功");
                }else{
                    System.out.println("插入失败");
                }
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        }finally {
            try {
                if(preparedStatement!=null){
                    preparedStatement.close();
                }
                if(connection!=null){
                    connection.close();
                }
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }

        }

    }
        private static void m2() {
            Scanner sc = new Scanner(System.in);
            String name=sc.nextLine();
            Connection connection = null;
            PreparedStatement preparedStatement = null;
            ResultSet resultSet = null;
            try {
                Class.forName("com.mysql.cj.jdbc.Driver");
                String url = "jdbc:mysql://127.0.0.1:3306/jdbc1?characterEncoding=utf-8";
                try {
                    connection = DriverManager.getConnection(url, "root", "130258");
//                if(connection !=null){
//                    System.out.println("链接成功");
//                }
                    String sql = "select *from user where name like ?";
                    preparedStatement = connection.prepareStatement(sql);
                    preparedStatement.setString(1, "%"+name+"%");

                    resultSet = preparedStatement.executeQuery();
                    while (resultSet.next()) {
                        int id = resultSet.getInt(1);
                        String username = resultSet.getString(2);
                        String pass = resultSet.getString(3);
                        System.out.println(id + "----" + username + "----" + pass);
                    }


                } catch (SQLException e) {
                    throw new RuntimeException(e);
                }
            } catch (ClassNotFoundException e) {
                throw new RuntimeException(e);
            } finally {
                try {
                    if (resultSet != null) {
                        resultSet.close();
                    }
                    if (preparedStatement != null) {
                        preparedStatement.close();
                    }
                    if (connection != null) {
                        connection.close();
                    }
                } catch (SQLException e) {
                    throw new RuntimeException(e);
                }

            }

        }
        private static void m4() {
            Connection connection=null;
            PreparedStatement preparedStatement=null;
            ResultSet resultSet=null;
            try {
                Class.forName("com.mysql.cj.jdbc.Driver");
                String url="jdbc:mysql://127.0.0.1:3306/jdbc1?characterEncoding=utf-8";
                try {
                    connection = DriverManager.getConnection(url, "root", "130258");
//                if(connection !=null){
//                    System.out.println("链接成功");
//                }
                    String sql="update user set name=? ,pass=? where id=?";
                    preparedStatement = connection.prepareStatement(sql);
                    resultSet = preparedStatement.executeQuery();
                    while(resultSet.next()){
                        int id = resultSet.getInt(1);
                        String name = resultSet.getString(2);
                        String pass= resultSet.getString(3);
                        preparedStatement.setString(1,"小明");
                        preparedStatement.setString(2,"123456");
                        preparedStatement.setInt(3,id);
                        System.out.println(id+"----"+name+"----"+pass);
                    }



                } catch (SQLException e) {
                    throw new RuntimeException(e);
                }
            } catch (ClassNotFoundException e) {
                throw new RuntimeException(e);
            }finally {
                try {
                    if(resultSet!=null){
                        resultSet.close();
                    }
                    if(preparedStatement!=null){
                        preparedStatement.close();
                    }
                    if(connection!=null){
                        connection.close();
                    }
                } catch (SQLException e) {
                    throw new RuntimeException(e);
                }

            }

        }
        private static void m5() {
            Connection connection=null;
            PreparedStatement preparedStatement=null;
            ResultSet resultSet=null;
            try {
                Class.forName("com.mysql.cj.jdbc.Driver");
                String url="jdbc:mysql://127.0.0.1:3306/jdbc1?characterEncoding=utf-8";
                try {
                    connection = DriverManager.getConnection(url, "root", "130258");
//                if(connection !=null){
//                    System.out.println("链接成功");
//                }
                    String sql="delete from user where id=? ";
                    preparedStatement = connection.prepareStatement(sql);
                    preparedStatement.setInt(1,7);



                } catch (SQLException e) {
                    throw new RuntimeException(e);
                }
            } catch (ClassNotFoundException e) {
                throw new RuntimeException(e);
            }finally {
                try {
                    if(resultSet!=null){
                        resultSet.close();
                    }
                    if(preparedStatement!=null){
                        preparedStatement.close();
                    }
                    if(connection!=null){
                        connection.close();
                    }
                } catch (SQLException e) {
                    throw new RuntimeException(e);
                }

            }
        }
    }
